package com.dynamixsoftware.printershare;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import com.dynamixsoftware.printershare.ActivityPrint;
import com.dynamixsoftware.printershare.App;
import java.util.Vector;

public class ActivityPrintText extends ActivityPrint {

	protected Canvas canvas;
	protected float fontSizeCoef;
	private float[] fontSizeCoefValues;
	private CharSequence[] fontSizeNames;
	protected int m;
	protected boolean need_new;
	protected int p_height;
	protected int p_width;
	protected Picture page;
	private int selectedFontSize = 1;
	protected int th;

	protected void addPage() {
		if (this.page != null) {
			this.page.endRecording();
			this.pages.add(new ActivityPrint.Page(this.page));
		}

	}

	protected void createPages() {
	}

	protected void initPage() {
		this.page = null;
		this.canvas = null;
		this.need_new = true;
		this.th = 0;
		this.p_width = this.paper.width;
		this.p_height = this.paper.height;
		if (this.orientation == 2) {
			int var1 = this.p_width;
			this.p_width = this.p_height;
			this.p_height = var1;
		}

		this.m = 0;
		if (this.margins == 1) {
			this.m = 63;
		} else if (this.margins == 2) {
			this.m = 84;
		} else if (this.margins == 3) {
			this.m = 127;
		}

		while (this.p_width < 500 || this.p_height < 500) {
			this.p_width = 2 * this.p_width;
			this.p_height = 2 * this.p_height;
		}

		this.pages = new Vector();
	}

	protected boolean needNew(int var1, int var2) {
		if (this.th > 0) {
			this.canvas.translate(0.0F, (float) var1 * this.fontSizeCoef);
			this.th = (int) ((float) this.th + (float) var1 * this.fontSizeCoef);
		}

		if ((float) this.th + (float) var2 * this.fontSizeCoef > (float) (-10 + (this.page.getHeight() - 2 * this.m))) {
			this.need_new = true;
			return true;
		} else {
			return false;
		}
	}

	protected void newPage() {
		if (this.need_new) {
			this.need_new = false;
			if (this.page != null) {
				this.page.endRecording();
				this.pages.add(new ActivityPrint.Page(this.page));
			}

			this.page = new Picture();
			this.canvas = this.page.beginRecording(this.p_width, this.p_height);
			this.canvas.drawColor(-1);
			this.canvas.translate((float) this.m, (float) this.m);
			this.th = 0;
		}

	}

	public void onCreate(Bundle var1) {
		super.onCreate(var1);
		Resources var2 = this.getResources();
		this.fontSizeNames = new CharSequence[3];
		this.fontSizeNames[0] = var2.getString(2131099830);
		this.fontSizeNames[1] = var2.getString(2131099831);
		this.fontSizeNames[2] = var2.getString(2131099832);
		this.fontSizeCoefValues = new float[3];
		this.fontSizeCoefValues[0] = 0.5F;
		this.fontSizeCoefValues[1] = 0.7F;
		this.fontSizeCoefValues[2] = 1.0F;
		this.fontSizeCoef = this.fontSizeCoefValues[this.selectedFontSize];
	}

	public void onCreateContextMenu(ContextMenu var1, View var2, ContextMenuInfo var3) {
		var1.add(0, 40, 0, 2131099790);
		super.onCreateContextMenu(var1, var2, var3);
	}

	public boolean onMenuItemSelected(int var1, MenuItem var2) {
		switch (var2.getItemId()) {
		case 40:
			(new Builder(this))
					.setTitle(2131099790)
					.setPositiveButton(2131099703, new OnClickListener() {
						public void onClick(DialogInterface var1, int var2) {
							ActivityPrintText.this.fontSizeCoef = ActivityPrintText.this.fontSizeCoefValues[ActivityPrintText.this.selectedFontSize];
							ActivityPrintText.this.need_update_pages = true;
							ActivityPrintText.this.update();
						}
					}).setNegativeButton(2131099707, (OnClickListener) null)
					.setSingleChoiceItems(this.fontSizeNames, this.selectedFontSize, new OnClickListener() {
						public void onClick(DialogInterface var1, int var2) {
							ActivityPrintText.this.selectedFontSize = var2;
						}
					}).show();
			return true;
		default:
			return super.onMenuItemSelected(var1, var2);
		}
	}

	protected void printRect(int var1, int var2, int var3, int var4, int var5) {
		Paint var6 = App.newPaint();
		var6.setStyle(Style.FILL);
		var6.setColor(var1);
		this.canvas.drawRect(new Rect(var2, var3, var4, var5), var6);
	}

	protected void printText(String var1, int var2, boolean var3, float var4, int var5, int var6, int var7, Paint var8) {
		int var9 = 0;
		String var10 = var1.replace("\r\n", "\n").replace("\t", " ");
		Typeface var11;
		if (var3) {
			var11 = Typeface.DEFAULT_BOLD;
		} else {
			var11 = Typeface.DEFAULT;
		}

		var8.setTypeface(var11);
		var8.setTextSize((float) var2 * this.fontSizeCoef);
		float[] var13 = new float[var10.length()];
		var8.getTextWidths(var10, 0, var10.length(), var13);
		float var15 = var4 * this.fontSizeCoef;
		int var16 = 0;
		float var17 = var4 * this.fontSizeCoef;
		if (this.needNew(0, var2)) {
			this.newPage();
		}

		for (int var18 = 0; var18 < var13.length; ++var18) {
			var15 += var13[var18];
			char var19 = var10.charAt(var18);
			if (var19 == 32 || var19 == 10 || var18 == -1 + var13.length) {
				float var20 = (float) (this.page.getWidth() - 2 * this.m) - (float) var5 * this.fontSizeCoef;
				float var21;
				if (var16 == 0) {
					var21 = var4 * this.fontSizeCoef;
				} else {
					var21 = 0.0F;
				}

				float var22 = var20 - var21;
				if (var15 < var22) {
					Canvas var24 = this.canvas;
					byte var25;
					if (var19 == 10) {
						var25 = 0;
					} else {
						var25 = 1;
					}

					var24.drawText(var10, var16, var18 + var25, var17 + (float) var5 * this.fontSizeCoef, (float) var6
							* this.fontSizeCoef, var8);
					var17 = var15;
					var16 = var18 + 1;
					if (var19 == 10 || var18 == -1 + var13.length) {
						this.translate(var2);
						var9 += var2;
						var17 = 0.0F;
						var15 = 0.0F;
					}
				} else if (var17 == 0.0F) {
					var15 = 0.0F;
					int var23 = var16;

					do {
						var15 += var13[var23];
						++var23;
					} while (var23 < var13.length && var15 < var22);

					if (var23 < -1 + var13.length) {
						--var23;
						var15 -= var13[var23];
					}

					this.canvas.drawText(var10, var16, var23, var17 + (float) var5 * this.fontSizeCoef, (float) var6
							* this.fontSizeCoef, var8);
					this.translate(var2);
					var9 += var2;
					var18 = var23 - 1;
					var16 = var18;
				} else {
					this.translate(var2);
					var9 += var2;
					var18 = var16 - 1;
					var17 = 0.0F;
					var15 = 0.0F;
				}
			}
		}

		if (var9 <= var7) {
			this.canvas.translate(0.0F, (float) (var7 - var9) * this.fontSizeCoef);
			this.th = (int) ((float) this.th + 1.0F + (float) (var7 - var9) * this.fontSizeCoef);
		} else {
			this.canvas.translate(0.0F, 10.0F * this.fontSizeCoef);
			this.th = (int) ((float) this.th + 1.0F + 10.0F * this.fontSizeCoef);
		}
	}

	protected int testTextSize(String var1, int var2, boolean var3, float var4, int var5) {
		int var6 = 0;
		String var7 = var1.replace("\r\n", "\n").replace("\t", " ");
		Paint var8 = App.newPaint();
		var8.setStyle(Style.FILL);
		var8.setColor(-16777216);
		Typeface var9;
		if (var3) {
			var9 = Typeface.DEFAULT_BOLD;
		} else {
			var9 = Typeface.DEFAULT;
		}

		var8.setTypeface(var9);
		var8.setTextSize((float) var2 * this.fontSizeCoef);
		float[] var11 = new float[var7.length()];
		var8.getTextWidths(var7, 0, var7.length(), var11);
		float var13 = var4 * this.fontSizeCoef;
		int var14 = 0;
		float var15 = var4 * this.fontSizeCoef;

		for (int var16 = 0; var16 < var11.length; ++var16) {
			var13 += var11[var16];
			char var17 = var7.charAt(var16);
			if (var17 == 32 || var17 == 10 || var16 == -1 + var11.length) {
				float var18 = (float) (this.page.getWidth() - 2 * this.m) - (float) var5 * this.fontSizeCoef;
				float var19;
				if (var14 == 0) {
					var19 = var4 * this.fontSizeCoef;
				} else {
					var19 = 0.0F;
				}

				float var20 = var18 - var19;
				if (var13 < var20) {
					var15 = var13;
					var14 = var16 + 1;
					if (var17 == 10 || var16 == -1 + var11.length) {
						var6 += var2;
						var15 = 0.0F;
						var13 = 0.0F;
					}
				} else if (var15 == 0.0F) {
					var13 = 0.0F;
					int var21 = var14;

					do {
						var13 += var11[var21];
						++var21;
					} while (var21 < var11.length && var13 < var20);

					if (var21 < -1 + var11.length) {
						--var21;
						var13 -= var11[var21];
					}

					var6 += var2;
					var16 = var21 - 1;
					var14 = var16;
				} else {
					var6 += var2;
					var16 = var14 - 1;
					var15 = 0.0F;
					var13 = 0.0F;
				}
			}
		}

		return var6;
	}

	protected void translate(int var1) {
		if ((float) this.th + (float) var1 * this.fontSizeCoef > (float) (-10 + (this.page.getHeight() - 2 * this.m))) {
			this.need_new = true;
			this.newPage();
		} else {
			this.canvas.translate(0.0F, (float) var1 * this.fontSizeCoef);
			this.th = (int) ((float) this.th + (float) var1 * this.fontSizeCoef);
		}
	}
}
